import java.util.*;

public class Solution986 {
    public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
        TreeSet<Integer> treeSet=new TreeSet<>();
        for (int[] ints : firstList) {
            for (int anInt : ints) {
                treeSet.add(anInt);
                treeSet.add(anInt+1);
                treeSet.add(anInt-1);
            }
        }
        for (int[] ints : secondList) {
            for (int anInt : ints) {
                treeSet.add(anInt);
                treeSet.add(anInt+1);
                treeSet.add(anInt-1);
            }
        }
        int id=0;
        Map<Integer,Integer> map=new HashMap<>();
        Map<Integer,Integer> map2=new HashMap<>();
        for (Integer integer : treeSet) {
            map.put(integer,id);
            map2.put(id,integer);
            id++;
        }
        int[] f=new int[id];
        int[] g=new int[id];
        int[] h=new int[id];
        for (int[] ints : firstList) {
            int l=map.get(ints[0]);
            int r=map.get(ints[1]);
            for (int i=l;i<r;i++){
                f[i]++;
            }
            h[l]++;
            g[r]++;
        }
        for (int[] ints : secondList) {
            int l=map.get(ints[0]);
            int r=map.get(ints[1]);
            for (int i=l;i<r;i++){
                f[i]++;
            }
            h[l]++;
            g[r]++;
        }
        List<int[]> ans=new ArrayList<>();
        for (int i=0;i<id;i++){
            if (f[i]==2){
                int a=i;
                while (i+1<id && f[i+1]==2){
                    i++;
                }
                int b=i;
                ans.add(new int[]{map2.get(a),map2.get(b+1)});
            }
            if (g[i]==1 && h[i]==1){
                ans.add(new int[]{map2.get(i),map2.get(i)});
            }
        }
        int[][] re=new int[ans.size()][2];
        for (int i=0;i<ans.size();i++){
            re[i]=ans.get(i);
        }
        return re;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.deepToString(new Solution986().intervalIntersection(new int[][]{{0, 2}, {5, 10}, {13, 23}, {24, 25}}, new int[][]{{1, 5}, {8, 12}, {15, 24}, {25, 26}})));
    }
}
